let express=require("express");
let fs=require("fs");
//文件上传
let multer=require("multer");
let app=new express();
//dest 存放文件的相对目录
app.use(multer({dest:"./public/tmp/"}).single("singleFile"))
app.get("/index",(req,res)=>{
    res.sendFile(__dirname+"/public/upload.html");
})
//路由
//文件上传 fs
app.post("/upload",(req,res)=>{
    //文件路径
    let filename="/tmp/"+req.file.filename;
    let filepath="./public"+filename;
    //重命名文件
    fs.rename(filepath,filepath+".jpg",(err)=>{
        if(err){
            console.log(err);
            return;
        }
    })
    res.writeHead(200,{"content-type":"text/html;charset=utf-8"});
    console.log(req.headers.origin);
    let path=req.headers.origin+filename+".jpg";
    console.log(path);
    res.end("<img src='"+path+"'/>");
})
app.use(express.static("public"));
app.listen(8080);